﻿/**
 * 返回所有用户自定义的属性
 * @name Object.keys
 * @function
 * @grammar Object.keys(obj)
 * @param {Object|Function} obj 需要检测的对象
 * 
 * @returns {Array} 自定义属性组成的数组
 */
if (!Object.keys) Object.keys = (function() {
  var hasOwnProperty = Object.prototype.hasOwnProperty,
    hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'),
    dontEnums = [ 'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'constructor' ],
    dontEnumsLength = dontEnums.length;
    return function(obj) {
      if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null)
        throw new TypeError('Object.keys called on non-object');
      var result = [];
      for(var prop in obj) {
        if (hasOwnProperty.call(obj, prop)) result.push(prop);
      }
      if (hasDontEnumBug) {
        for(var i=0; i < dontEnumsLength; i++) {
          if (hasOwnProperty.call(obj, dontEnums[i])) result.push(dontEnums[i]);
        }
      }
      return result;
    }
  }
)();